<script>
	var jpushModule = uni.requireNativePlugin("JG-JPush");
	import options from '@/common/config/options.js';
	import initLevitatedSphere from '@/common/config/levitatedSphere.js';

	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				activeCtx: null,
			}
		},
		onLaunch: function() {
			// console.log('App Launch')
			// console.log(this.$store.state.openMsgBtnType)

			// 获取全局按钮实例对象
			const activeCtx = initLevitatedSphere(options, this.clickHandler, false);

			this.activeCtx = activeCtx;
			// 存储到vuex
			this.$store.commit('ChangeAndSetStateData', {
				Name1: 'activeCtx',
				data: activeCtx,
			});

			try {
				if (uni.getSystemInfoSync().platform == "ios") {

					// 请求定位权限
					let locationServicesEnabled = jpushModule.locationServicesEnabled()
					let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
					console.log('locationServicesEnabled', locationServicesEnabled)
					console.log('locationAuthorizationStatus', locationAuthorizationStatus)
					if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
						jpushModule.requestLocationAuthorization((result) => {
							console.log('定位权限', result.status)
						})
					}



					jpushModule.requestNotificationAuthorization((result) => {
						console.log(result)
						let status = result.status
						if (status < 2) {
							uni.showToast({
								icon: 'none',
								title: '您还没有打开通知权限',
								duration: 3000
							})
						}
					})
				}

				jpushModule.getRegistrationID(result => {
					// let registerID = result.registerID
				})

				jpushModule.initJPushService();
				jpushModule.setLoggerEnable(true);
				jpushModule.addConnectEventListener(result => {
					let connectEnable = result.connectEnable
					uni.$emit('connectStatusChange', connectEnable)
				});


				// 接收消息（通知栏消息 + 应用内接收消息）
				jpushModule.addNotificationListener(result => {
					let notificationEventType = result.notificationEventType
					let messageID = result.messageID
					let title = result.title
					let content = result.content
					let extras = result.extras
					console.log(result)

					if (notificationEventType == 'notificationArrived') { // 收到消息

					} else if (notificationEventType == 'notificationOpened') {
						// 手动点击消息
						jpushModule.setBadge(0)
					}
				});

				// 自定义消息事件回调-监听 Portal 发送的自定义消息
				jpushModule.addCustomMessageListener(result => {
					// let type = result.type
					// let messageType = result.messageType
					// let content = result.content
				})

				// 本地通知事件回调
				jpushModule.addLocalNotificationListener(result => {
					console.log(result)
				})

				// jpushModule.addGeofenceListener(result => {
				// 	let code = result.code
				// 	let type = result.type
				// 	let geofenceId = result.geofenceId
				// 	let userInfo = result.userInfo
				// 	console.log(result)
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '触发地理围栏',
				// 		duration: 3000
				// 	})
				// })

				jpushModule.setIsAllowedInMessagePop(true)
				// jpushModule.pullInMessage(result => {
				// 	let code = result.code
				// 	console.log(code)
				// })

				// 应用内-插屏消息
				jpushModule.addInMessageListener(result => {
					console.log(result)
					let eventType = result.eventType
					let messageType = result.messageType
					let content = result.content
					console.log('inMessageListener', eventType, messageType, content)
				})
			} catch (e) {
				//TODO handle the exception
			}
		},
		onShow: function() {
			// console.log('App Show')



			// 获取本地应用资源版本号
			// plus.runtime.getProperty(plus.runtime.appid, (inf) => {
			// 	console.log(inf)
			// })

		},
		onHide: function() {
			// console.log('App Hide')
		},
		created() {},
		computed: mapState({
			openMsgBtnType(state) {
				return state.openMsgBtnType
			}
		}),
		watch: {
			openMsgBtnType: {
				handler(nVal, oVal) {
					let Data = null;
					if (nVal == 'true') {
						Data = this.activeCtx;
					} else {
						if (this.$store.state.activeCtx) {
							this.$store.state.activeCtx.hide();
							Data = null;
						}
					}

					this.$store.commit('ChangeAndSetStateData', {
						Name1: 'activeCtx',
						data: Data,
					});

				}
			},
		},
		methods: {
			clickHandler(res) {
				if (res.type == 'click') {
					console.log('click', res);
					// uni.showToast({
					// 	title: '点击事件!'
					// });

					uni.navigateTo({
						url: '/pages/Message/Message?nav=1'
					})

					this.$store.state.activeCtx.hide();
				}
				if (res.type == 'move') {

					console.log('move', res);
				}
			}
		}
	}
</script>


<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uview-ui/index.scss';
	// @import '@/static/assets/css/font/iconfont.css';

	/* #ifndef APP-NVUE */

	@font-face {
		font-family: CustomFontPlatform;
		src: url('@/static/assets/css/platformfont/iconfont.ttf') format('truetype');
		// src: url('http://mes.hzyc.org.cn:20050/icons/iconfont.ttf') format('truetype');
	}


	/* #endif*/
</style>